var express = require('express');
var router = express.Router();

var async = require('async');
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = "mongodb://localhost:27017/Movie";
/* GET home page. */
router.get('/', function (req, res) {
  async.parallel([
    // coming_soon
    function (cb) {

      //分页信息
      var pageNum_cs = req.query.pageNum_cs,
        pageNum_cs = pageNum_cs ? pageNum_cs : 1,
        pageSize = 6,
        count = 0,
        totalpages = 0;

      MongoClient.connect(DB_CONN_STR, function (err, db) {
        if (err) {
          return;
        } else {
          var doc = 'movie_coming_soon'
          var conn = db.collection(doc);
          var data = {}
          async.parallel([
            function (cb) {
              conn.find({}).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  totalpages = Math.ceil(results.length / pageSize);
                  count = results.length;
                  data.totalpages=totalpages;
                  data.count=count;
                  cb(null, '');
                }
              });
            },
            function (cb) {
              conn.find({}).sort({ _id: -1 }).skip((pageNum_cs - 1) * pageSize).limit(pageSize).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  cb(null, results);
                }
              });

            }
          ], function (err, results) {
            results.data=data;
            cb(null, results);
          });
          
          // cb(null, data);
        }
      });
    },
    // top250      
    function (cb) {

      //分页信息
      var pageNum_top = req.query.pageNum_top,
        pageNum_top = pageNum_top ? pageNum_top : 1,
        pageSize = 6,
        count = 0,
        totalpages = 0;

      MongoClient.connect(DB_CONN_STR, function (err, db) {
        if (err) {
          return;
        } else {
          var doc = 'movie_top250'
          var conn = db.collection(doc);
          var data = {}
          async.parallel([
            function (cb) {
              conn.find({}).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  totalpages = Math.ceil(results.length / pageSize);
                  count = results.length;
                  data.totalpages=totalpages;
                  data.count=count;
                  cb(null, '');
                }
              });
            },
            function (cb) {
              conn.find({}).sort({ _id: -1 }).skip((pageNum_top - 1) * pageSize).limit(pageSize).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  cb(null, results);
                }
              });

            }
          ], function (err, results) {
            results.data=data;
            cb(null, results);
          });
          
          // cb(null, data);
        }
      });
    },
    // in_theaters
     function (cb) {

      //分页信息
      var pageNum_in= req.query.pageNum_in,
        pageNum_in = pageNum_in ? pageNum_in : 1,
        pageSize = 6,
        count = 0,
        totalpages = 0;

      MongoClient.connect(DB_CONN_STR, function (err, db) {
        if (err) {
          return;
        } else {
          var doc = 'movie_in_theaters'
          var conn = db.collection(doc);
          var data = {}
          async.parallel([
            function (cb) {
              conn.find({}).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  totalpages = Math.ceil(results.length / pageSize);
                  count = results.length;
                  data.totalpages=totalpages;
                  data.count=count;
                  cb(null, '');
                }
              });
            },
            function (cb) {
              conn.find({}).sort({ _id: -1 }).skip((pageNum_in - 1) * pageSize).limit(pageSize).toArray(function (err, results) {
                if (err) {
                  console.log(err);
                } else {
                  cb(null, results);
                }
              });

            }
          ], function (err, results) {
            results.data=data;
            cb(null, results);
          });
          
          // cb(null, data);
        }
      });
    },
    //session
    function (cb) {
      cb(null, req.session.email);
    }
  ], function (err, results) {
    res.render('index', {
      movie: {
        movie_coming_soon: results[0],
        movie_top250: results[1],
        movie_in_theater: results[2],
        email: results[3]
      }
    });
  });
}); //index第一个参数指代的是模板页 views/index.ejs


router.get('/login', function (req, res, next) {
  res.render('login'); //render 渲染
});
router.get('/register', function (req, res, next) {
  res.render('register'); //render 渲染
});
module.exports = router;

//ejs的模板渲染，=输出变量，-输出html内容